package com.huawei.hms.framework.network.restclient.dnkeeper;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.PLSharedPreferences;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsResult;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsResult.Address.Builder;
import com.huawei.hms.framework.network.util.ContextUtil;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DNKeeperUtil {
    private static final String TAG = "DNKeeperCallable";

    public static String getRequestStr(RequestHost requestHost) {
        Context context = ContextUtil.getContext();
        String str = "";
        if (requestHost != null) {
            JSONObject jSONObject = new JSONObject();
            Object apkName = requestHost.getApkName();
            if (TextUtils.isEmpty(apkName) && context != null) {
                apkName = context.getPackageName();
            }
            try {
                jSONObject.put("DomainName", requestHost.getDomainName());
                jSONObject.put("ApkName", apkName);
                jSONObject.put("DnsFailType", requestHost.getDnsFailType());
                jSONObject.put("FailIP", requestHost.getFailIP());
                str = jSONObject.toString();
            } catch (Throwable e) {
                Logger.w(TAG, "getRequestStr", e);
            }
        }
        return str;
    }

    public static void getArrayaddress(JSONArray jSONArray, int i, String str, String str2, long j) throws JSONException {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("type", str);
            jSONObject.put("value", str2);
            jSONObject.put("ttl", j);
            jSONArray.put(i, jSONObject);
        } catch (Throwable e) {
            Logger.w(TAG, "JSONException", e);
            throw e;
        }
    }

    public static DnsResult parseResponse(String str) {
        Throwable e;
        Logger.v(TAG, "parseResponse: " + str);
        DnsResult dnsResult = new DnsResult();
        if (TextUtils.isEmpty(str)) {
            return dnsResult;
        }
        JSONObject jSONObject = new JSONObject(str);
        String string = jSONObject.getString("type");
        dnsResult.setType(string);
        JSONArray jSONArray = jSONObject.getJSONArray("addressList");
        dnsResult.setCreateTime(jSONObject.getLong("createTime"));
        int length = jSONArray.length();
        for (int i = 0; i < length; i++) {
            jSONObject = (JSONObject) jSONArray.get(i);
            String string2 = jSONObject.getString("value");
            if (!TextUtils.isEmpty(string2)) {
                if ("CNAME".equals(string)) {
                    dnsResult.setType("A");
                    try {
                        InetAddress[] allByName = InetAddress.getAllByName(string2);
                        if (allByName != null && allByName.length > 0) {
                            for (InetAddress inetAddress : allByName) {
                                dnsResult.addAddress(new Builder().value(inetAddress.getHostAddress()).ttl(jSONObject.getLong("ttl") * 1000).type(inetAddress instanceof Inet4Address ? "A" : "AAAA").build());
                            }
                        }
                    } catch (UnknownHostException e2) {
                        try {
                            Logger.w(TAG, "DNKeeperManager parseResponse failed,UnknownHostException:" + string2);
                        } catch (Throwable e3) {
                            Logger.w(TAG, "parseResponse", e3);
                            return dnsResult;
                        }
                    } catch (IllegalArgumentException e4) {
                        e3 = e4;
                        Logger.w(TAG, "DNKeeperManager parseResponse failed, Exception: " + string2, e3);
                    } catch (NullPointerException e5) {
                        e3 = e5;
                        Logger.w(TAG, "DNKeeperManager parseResponse failed, Exception: " + string2, e3);
                    }
                } else {
                    dnsResult.addAddress(new Builder().value(string2).type(jSONObject.getString("type")).ttl(jSONObject.getLong("ttl") * 1000).build());
                }
            }
        }
        return dnsResult;
    }

    public static boolean checkDNKeeperIP(PLSharedPreferences pLSharedPreferences, String str) {
        if (pLSharedPreferences == null) {
            return true;
        }
        DnsResult parseResponse = parseResponse(pLSharedPreferences.getString(str));
        if (parseResponse == null || isIpListEmpty(parseResponse)) {
            return true;
        }
        if (System.currentTimeMillis() - parseResponse.getCreateTime() < DNKeeperConfig.SEVEN_DAYS) {
            return false;
        }
        return true;
    }

    public static boolean isIpListEmpty(DnsResult dnsResult) {
        return dnsResult == null || dnsResult.isEmpty();
    }

    public static DnsResult parseDnsResult(DnsResult dnsResult, String str) {
        if (!TextUtils.isEmpty(str)) {
            List arrayList = new ArrayList();
            arrayList.add(new Builder().value(str).build());
            dnsResult.setAddressList(arrayList);
        }
        return dnsResult;
    }
}
